/*
 * @Descripttion: 
 * @version: 
 * @Author: LiuXuFei
 * @Date: 2021-11-24 12:43:52
 * @LastEditors: linZeJian
 * @LastEditTime: 2022-11-02 14:08:31
 */
import request from "./customUni"

const service = request.create({
	// axios中请求配置有baseURL选项，表示请求URL公共部分
	// 小程序url地址得写全，配置在具体接口跟condig文件
	// baseURL: "https://spc-wxapplet1.bu.gzsunrun.cn",
	baseURL: "",
	// 超时
	timeout: 100000,

})
// request拦截器
service.interceptors.request.use(res => {

	uni.showLoading({
		title: ''
	});

	let token = uni.getStorageSync('token');
	if (token) {
		res.header.Authorization = 'Session ' + token
	}
	// 应用服务本地启动带token
	res.header["X-Oars-Token"] = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySUQiOiJsemoiLCJleHAiOjE2NzAzNzgyMTQsImlzcyI6Im9hcnMtaWRhYXMiLCJzdWIiOiJ7XCJ1c2VySWRcIjpcImx6alwiLFwidXNlcm5hbWVcIjpcImx6alwiLFwibmlja05hbWVcIjpcIuael-azveWdmlwiLFwiZGVwdElkXCI6XCIxMzg4MDIxMzI2XCIsXCJkZXB0UGF0aFwiOlwiLzEvMTM4ODAyMTMyNlwiLFwiYXBwSWRcIjpcIjg3MWFjMmUzLTk0NTEtNDhhYi04Y2FlLTc0YzM1MTIwYTQxOVwiLFwicmVhbElkXCI6XCJcIixcImtpbmRcIjowLFwidmVyaWZpZWRcIjpmYWxzZX0ifQ.DPgd13CW-AeRqW_4J0xvStPaSxL4y1y7nS5gzUcZD7I"
	res.header["Authorization"] = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySUQiOiJsemoiLCJleHAiOjE2NzAzNzgyMTQsImlzcyI6Im9hcnMtaWRhYXMiLCJzdWIiOiJ7XCJ1c2VySWRcIjpcImx6alwiLFwidXNlcm5hbWVcIjpcImx6alwiLFwibmlja05hbWVcIjpcIuael-azveWdmlwiLFwiZGVwdElkXCI6XCIxMzg4MDIxMzI2XCIsXCJkZXB0UGF0aFwiOlwiLzEvMTM4ODAyMTMyNlwiLFwiYXBwSWRcIjpcIjg3MWFjMmUzLTk0NTEtNDhhYi04Y2FlLTc0YzM1MTIwYTQxOVwiLFwicmVhbElkXCI6XCJcIixcImtpbmRcIjowLFwidmVyaWZpZWRcIjpmYWxzZX0ifQ.DPgd13CW-AeRqW_4J0xvStPaSxL4y1y7nS5gzUcZD7I"
	console.log(res, "请求信息")
	return res
})

// 响应拦截器
service.interceptors.response.use(res => {
	console.log('res0000', res)
	uni.hideLoading();
	let errcode = {
		'401': '认证失败，无法访问系统资源',
		'403': '当前操作没有权限',
		'404': '访问资源不存在',
		'default': '系统未知错误，请反馈给管理员'
	}
	if (res.statusCode != 200 && res.statusCode != 10000) {
		let errMsg = res.errMsg
		let message = errcode[res.statusCode] || errcode.default

		if (errMsg == "Network Error") {
			message = "后端接口连接异常";
		} else if (errMsg.includes("timeout")) {
			message = "系统接口请求超时";
		} else if (errMsg.includes("Request failed with status code")) {
			message = "系统接口" + message.substr(message.length - 3) + "异常";
		}
		uni.showToast({
			icon: "error",
			title: message,
			duration: 2000
		});
		return Promise.reject("err")
	}


	if (res.data.code == 401) {
		uni.showModal({
			title: '提示',
			content: '用户未登录，请先登录！',
			success: function (res) {
				if (res.confirm) {
					console.log('用户点击确定');
					uni.navigateTo({
						url: "/pages/login/index",
					});
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
	} else if (res.data.code != 10000 && res.data.code != 200 && res.data.code != 50002) {
		uni.showToast({
			icon: "error",
			title: res.data.message || res.data.msg,
			duration: 3000
		});
		console.log(res.data)
		return Promise.reject("接口错误")
	}
	return res.data
})

export default service